﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.Schedule
{
    /// <summary>
    /// TaskList 的摘要说明。
    /// </summary>
    public partial class TaskListPage : Page
    {
        public static int displayType;
        public static string SchBeginDate;
        protected string Action;
        public string ActualUsername = "";
        public string Username = "";

        protected override void OnInit(EventArgs e)
        {
            btnAccept.Click += btnAccept_Click;
            btnCancel.Click += btnCancel_Click;
            btnSubscribe.Click += btnSubscribe_Click;
            listStaff.SelectedIndexChanged += listStaff_SelectedIndexChanged;
            lnkbtnToday.Click += lnkbtnToday_Click;
            lnkbtnHistory.Click += lnkbtnHistory_Click;
            lnkbtnFinished.Click += lnkbtnFinished_Click;
            Linkbutton1.Click += Linkbutton1_Click;
            lnkbtnArranged.Click += lnkbtnArranged_Click;
            btnWeeklyView.Click += btnWeeklyView_Click;
            btnCancelSubscription.Click += btnCancelSubscription_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["Username"] == null)
                {
                    Session["Username"] = SiteContext.Current.UserName;
                }
                ActualUsername = SiteContext.Current.UserName;
                Session["ActualUsername"] = ActualUsername;
                Username = (string)Session["Username"];

                populateData();
                SetStatus();
                displayType = Request.QueryString["displayType"] != null ? Int32.Parse(Request.QueryString["displayType"]) : 1;
                SchBeginDate = Request.QueryString["SchBeginDate"] != null ? Request.QueryString["SchBeginDate"] : DateTime.Today.AddDays(-(Convert.ToInt32(DateTime.Today.DayOfWeek)) + 1).ToShortDateString();
                if (Request.QueryString["SchBeginDate"] != null)
                {
                    lnkbtnFinished.Enabled = false;
                    lnkbtnHistory.Enabled = false;
                    lnkbtnToday.Enabled = false;
                    Linkbutton1.Enabled = false;
                    btnAccept.Enabled = false;
                    btnCancel.Enabled = false;
                    btnNew.Enabled = false;
                    lnkbtnArranged.Enabled = false;
                    btnWeeklyView.Text = "返回";
                    PopulateWeeklyData();
                }
                else
                {
                    InitData();
                }


                //Response.Write(DateTime.Today.AddDays(-(Convert.ToInt32(DateTime.Today.DayOfWeek))+1).ToShortDateString()); 
            }

            //Response.Write(Session["Username"].ToString());
            base.OnLoad(e);
        }

        private void InitData()
        {
            Username = (string)Session["Username"];

            tblContainer.Rows[0].Cells[0].Attributes.Add("background", "../../images/maillistbutton2.gif");
            btnNew.Attributes["onclick"] = "var newwin=window.open('Manage.aspx','newtask','toolbar=yes,scrollbars=yes,width=800,height=600,resizable=yes');newwin.moveTo(0,0);newwin.focus();";


            var task = new Task();
            dgList.Visible = true;
            btnAccept.Visible = true;
            btnCancel.Visible = true;

            //this.lnkbtnAdd.Attributes.Add("onclick","return dialwinprocess('"+DateTime.Today.ToShortDateString()+"','8','1','0')");
            //this.lnkbtnAdd.Attributes.Add("onclick","self.location='CalendarView.aspx?Action=1';return true;");
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetAllTaskBySomeone(DateTime.Today.ToShortDateString(), Username, displayType));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            setgrid();
            mydb.Dispose();
            for (int t = 0; t < 4; t++)
            {
                if (t == displayType - 1)
                {
                    tblContainer.Rows[0].Cells[t].Attributes.Add("background", "../../images/maillistbutton2.gif");
                }
                else
                {
                    tblContainer.Rows[0].Cells[t].Attributes.Add("background", "../../images/maillistbutton1.gif");
                }
            }
        }

        private void PopulateWeeklyData()
        {
            dgList.Visible = false;
            lblInstru.Visible = false;
            Table1.Visible = true;


            Table1.Rows.Clear();
            //Response.Write("<table width=\"49%\" border=\"1\" align=\"center\" class=\"top\"><tr><td width=\"2%\"  bgcolor=\"#A692F5\">&nbsp;</td>	<td width=\"18%\">未确认任务</td> <td width=\"2%\" bgcolor=\"BlanchedAlmond\">&nbsp;</td> <td width=\"18%\">已确认任务</td></tr></table>");
            //System.Globalization.DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek = System.DayOfWeek.Monday;
            //System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames=new String[]{"七","一","二","三","四","五","六"};
            PopulateDateToTable(DateTime.Parse(SchBeginDate), 6);
        }

        public void SetStatus()
        {
            var Username = (string)Session["Username"];
            var ActualUsername = (string)Session["ActualUsername"];

            btnAccept.Enabled = (Username.ToLower() == ActualUsername.ToLower());
            btnCancel.Enabled = (Username.ToLower() == ActualUsername.ToLower());

            btnNew.Enabled = (Username.ToLower() == ActualUsername.ToLower());
            btnSubscribe.Visible = (Username.ToLower() != ActualUsername.ToLower());

            btnSubscribe.Enabled = (Username.ToLower() != ActualUsername.ToLower());
        }

        public void populateData()
        {
            #region 协同人员列表初始化

            var Username = (string)Session["Username"];
            try
            {
                var staffList = Staff.GetStaffFromPosition(ActualUsername);
                listStaff.Items.Clear();
                foreach (var staff in staffList)
                {
                    listStaff.Items.Add(new ListItem(staff.RealName, staff.Name));
                }
                foreach (ListItem li in listStaff.Items)
                {
                    if (string.Compare(li.Value, Username, true) == 0)
                    {
                        li.Selected = true;
                    }
                }
                //				listStaff.SelectedItem .Value = Username;
                //				listStaff.SelectedItem .Text = UDS.Components .Staff.GetRealNameByUsername(Username);
            }
            catch (Exception e)
            {
                Logger.Log(e.ToString());
                Server.Transfer("../Error.aspx");
            }

            #endregion
        }


        public string GetStatus(string str)
        {
            switch (str)
            {
                case "0":
                    return "?"; // 待定
                case "1":
                    return "!"; // 待办
                case "2":
                    return "√"; // 完成
                default:
                    return "";
            }
        }

        public string GetPeriodByPeriodID(string EndTime, string begintime, string endtime)
        {
            if (begintime == "0" && endtime == "0")
            {
                return DateTime.Parse(EndTime).ToShortTimeString() + "(截止)";
            }
            if (begintime == "1" && endtime == "20")
            {
                return "(全天)";
            }
            int b = Int32.Parse(begintime);
            int e = Int32.Parse(endtime);
            var dt = new DateTime(1999, 1, 1, 8, 0, 0, 0);
            var ts = new TimeSpan(0, 0, (b - 1) * 30, 0, 0);
            DateTime bt = dt.Add(ts);
            DateTime et = bt.Add(new TimeSpan(0, 0, (e - b + 1) * 30, 0, 0));
            return bt.ToShortTimeString() + "-" + et.ToShortTimeString();
        }

        public string GetRealName(string Username)
        {
            if (Username != "")
            {
                return Components.Staff.GetRealNameByUsername(Username);
            }
            return "";
        }

        public string GetRealNameStr(string Username)
        {
            if (Username != "")
            {
                return Components.Staff.GetRealNameForStaffList(Username, 3);
            }
            return "";
        }


        public string GetType(string type)
        {
            string Type = "";
            if (type != "")
            {
                switch (Int32.Parse(type))
                {
                    case 1:
                        Type = "会议";
                        break;
                    case 2:
                        Type = "文案";
                        break;
                    case 3:
                        Type = "来访";
                        break;
                    case 4:
                        Type = "电话";
                        break;
                    case 5:
                        Type = "走访";
                        break;
                    case 6:
                        Type = "外出";
                        break;
                    case 7:
                        Type = "假期";
                        break;
                    case 8:
                        Type = "出差";
                        break;
                }
                return Type;
            }
            return "";
        }

        public string GetProjectName(string project)
        {
            if (project != "")
            {
                return ProjectClass.GetProjectName(Int32.Parse(project));
            }
            return "";
        }

        private void setgrid()
        {
            foreach (DataGridItem dgi in dgList.Items)
            {
                var lb = (Label)(dgi.Cells[3].Controls[1]);
                if (lb.Text == "?")
                {
                    dgi.BackColor = Color.AliceBlue;
                }
            }
        }

#if false
        private void btnList_Click(object sender, EventArgs e)
        {
            var Username = (string) Session["Username"];
            var task = new Task();
            dgList.Visible = true;
            btnCancel.Visible = true;
            btnAccept.Visible = true;
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetTodayTaskBySomeone(DateTime.Today.ToShortDateString(), Username, 1));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            setgrid();
            mydb.Dispose();
        }

        private void lnkbtnAccept_Click(object sender, EventArgs e)
        {
            var Username = (string) Session["Username"];
            string ids = "";
            foreach (DataGridItem dgi in dgList.Items)
            {
                var cb = (CheckBox) (dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    ids += dgList.DataKeys[dgi.ItemIndex] + ",";
                }
            }

            if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
                var task = new Task();
                try
                {
                    task.DealTask(ids, 1, Username, DateTime.Today.ToShortDateString());
                    Response.Write("<script language=javascript>alert('操作成功!');window.location='TaskList.aspx';</script>");
                    //Response.AddHeader("Refresh","1");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }

        private void lnkbtnCancel_Click(object sender, EventArgs e)
        {
            var Username = (string) Session["Username"];
            string ids = "";
            foreach (DataGridItem dgi in dgList.Items)
            {
                var cb = (CheckBox) (dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    ids += dgList.DataKeys[dgi.ItemIndex] + ",";
                }
            }

            if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
                var task = new Task();
                try
                {
                    task.DealTask(ids, 0, Username, DateTime.Today.ToShortDateString());
                    Response.Write("<script language=javascript>alert('操作成功!');window.location='TaskList.aspx';</script>");
                    //Response.AddHeader("Refresh","1");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }

        private void lnkbtnFinish_Click(object sender, EventArgs e)
        {
            var Username = (string) Session["Username"];
            string ids = "";
            foreach (DataGridItem dgi in dgList.Items)
            {
                var cb = (CheckBox) (dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    ids += dgList.DataKeys[dgi.ItemIndex] + ",";
                }
            }

            if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
                var task = new Task();
                try
                {
                    task.DealTask(ids, 2, Username, DateTime.Today.ToShortDateString());
                    Response.Write("<script language=javascript>alert('操作成功!');window.location='TaskList.aspx';</script>");
                    //Response.AddHeader("Refresh","1");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }
#endif

        private void lnkbtnToday_Click(object sender, EventArgs e)
        {
            var Username = (string)Session["Username"];
            btnCancelSubscription.Visible = false;
            btnCancel.Visible = true;
            displayType = 1;
            tblContainer.Rows[0].Cells[0].Attributes.Add("background", "../../images/maillistbutton2.gif");
            tblContainer.Rows[0].Cells[1].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[2].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[3].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[4].Attributes.Add("background", "../../images/maillistbutton1.gif");
            var task = new Task();
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetAllTaskBySomeone(DateTime.Today.ToShortDateString(), Username, displayType));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            setgrid();
            mydb.Dispose();
        }

        private void lnkbtnHistory_Click(object sender, EventArgs e)
        {
            var Username = (string)Session["Username"];
            btnCancelSubscription.Visible = false;
            btnCancel.Visible = true;
            displayType = 2;
            tblContainer.Rows[0].Cells[1].Attributes.Add("background", "../../images/maillistbutton2.gif");
            tblContainer.Rows[0].Cells[0].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[2].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[3].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[4].Attributes.Add("background", "../../images/maillistbutton1.gif");
            var task = new Task();
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetAllTaskBySomeone(DateTime.Today.ToShortDateString(), Username, displayType));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            setgrid();
            mydb.Dispose();
        }

        private void lnkbtnFinished_Click(object sender, EventArgs e)
        {
            var Username = (string)Session["Username"];
            btnCancelSubscription.Visible = false;
            btnCancel.Visible = false;
            displayType = 3;
            tblContainer.Rows[0].Cells[2].Attributes.Add("background", "../../images/maillistbutton2.gif");
            tblContainer.Rows[0].Cells[0].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[1].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[3].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[4].Attributes.Add("background", "../../images/maillistbutton1.gif");
            var task = new Task();
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetAllTaskBySomeone(DateTime.Today.ToShortDateString(), Username, displayType));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            //	setgrid();
            mydb.Dispose();
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            var Username = (string)Session["Username"];
            string ids = "";
            foreach (DataGridItem dgi in dgList.Items)
            {
                var cb = (CheckBox)(dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    ids += dgList.DataKeys[dgi.ItemIndex] + ",";
                }
            }

            if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
                var task = new Task();
                try
                {
                    task.DealTask(ids, 1, Username, DateTime.Today.ToShortDateString());
                    Response.Write("<script language=javascript>alert('操作成功!');window.location='TaskList.aspx';</script>");
                    //Response.AddHeader("Refresh","1");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            var Username = (string)Session["Username"];
            string ids = "";
            foreach (DataGridItem dgi in dgList.Items)
            {
                var cb = (CheckBox)(dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    ids += dgList.DataKeys[dgi.ItemIndex] + ",";
                }
            }

            if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
                var task = new Task();
                try
                {
                    task.DealTask(ids, 0, Username, DateTime.Today.ToShortDateString());
                    Response.Write("<script language=javascript>alert('操作成功!');window.location='TaskList.aspx';</script>");
                    //Response.AddHeader("Refresh","1");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }

#if false
        private void btnFinish_Click(object sender, EventArgs e)
        {
            var Username = (string)Session["Username"];
            string ids = "";
            foreach (DataGridItem dgi in dgList.Items)
            {
                var cb = (CheckBox)(dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    ids += dgList.DataKeys[dgi.ItemIndex] + ",";
                }
            }

            if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
                var task = new Task();
                try
                {
                    task.DealTask(ids, 2, Username, DateTime.Today.ToShortDateString());
                    Response.Write("<script language=javascript>alert('操作成功!');window.location='TaskList.aspx';</script>");
                    //Response.AddHeader("Refresh","1");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }
#endif

        public void listStaff_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["Username"] = listStaff.SelectedItem.Value;
            var Username = (string)Session["Username"];
            var ActualUsername = (string)Session["ActualUsername"];
            if (btnWeeklyView.Text == "周览")
            {
                Response.Redirect("TaskList.aspx?displayType=1");
            }
            else
            {
                lnkbtnFinished.Enabled = false;
                lnkbtnHistory.Enabled = false;
                lnkbtnToday.Enabled = false;
                Linkbutton1.Enabled = false;
                btnAccept.Enabled = false;
                btnCancel.Enabled = false;
                btnNew.Enabled = false;
                btnWeeklyView.Text = "返回";
                PopulateWeeklyData();
                SetStatus();
                btnSubscribe.Enabled = false;
            }
        }

        private void btnSubscribe_Click(object sender, EventArgs e)
        {
            var Username = (string)Session["Username"];
            var ActualUsername = (string)Session["ActualUsername"];
            string ids = "";
            var task = new Task();
            foreach (DataGridItem dgi in dgList.Items)
            {
                var cb = (CheckBox)(dgi.Cells[0].Controls[1]);
                var lb = (Label)(dgi.Cells[1].Controls[3]);
                if (cb.Checked)
                {
                    try
                    {
                        ids = dgList.DataKeys[dgi.ItemIndex].ToString();
                        if (task.AddTaskSubscription(ActualUsername, Username, Int32.Parse(ids), Convert.ToDateTime(lb.Text).ToShortDateString()))
                        {
                            Response.Write("<script language=javascript>alert('订阅成功!');window.location='TaskList.aspx?displayType=" + displayType + "';</script>");
                        }
                    }
                    catch (Exception dbex)
                    {
                        Logger.Log(dbex.ToString());
                        Server.Transfer("../Error.aspx");
                    }
                }
            }
        }


        private void Linkbutton1_Click(object sender, EventArgs e)
        {
            var ActualUsername = (string)Session["ActualUsername"];
            var Username = (string)Session["Username"];
            btnCancelSubscription.Visible = (ActualUsername.ToLower() == Username.ToLower());
            displayType = 4;
            tblContainer.Rows[0].Cells[3].Attributes.Add("background", "../../images/maillistbutton2.gif");
            tblContainer.Rows[0].Cells[0].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[1].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[2].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[4].Attributes.Add("background", "../../images/maillistbutton1.gif");
            var task = new Task();
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetAllTaskBySomeone(DateTime.Today.ToShortDateString(), Username, displayType));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            //	setgrid();
            mydb.Dispose();
        }

        private void btnWeeklyView_Click(object sender, EventArgs e)
        {
            if (btnWeeklyView.Text == "周览")
            {
                lnkbtnFinished.Enabled = false;
                lnkbtnHistory.Enabled = false;
                lnkbtnToday.Enabled = false;
                Linkbutton1.Enabled = false;
                btnAccept.Enabled = false;
                btnCancel.Enabled = false;
                btnNew.Enabled = false;
                lnkbtnArranged.Enabled = false;
                btnWeeklyView.Text = "返回";
                PopulateWeeklyData();
            }
            else if (btnWeeklyView.Text == "返回")
            {
                lnkbtnFinished.Enabled = true;
                lnkbtnHistory.Enabled = true;
                lnkbtnToday.Enabled = true;
                Linkbutton1.Enabled = true;
                lnkbtnArranged.Enabled = true;
                btnAccept.Enabled = true;
                btnCancel.Enabled = true;
                btnNew.Enabled = true;
                lblInstru.Visible = true;
                btnWeeklyView.Text = "周览";
                InitData();
            }
            SetStatus();
        }

        private void btnCancelSubscription_Click(object sender, EventArgs e)
        {
            var ActualUsername = (string)Session["ActualUsername"];
            string ids = "";
            foreach (DataGridItem dgi in dgList.Items)
            {
                var cb = (CheckBox)(dgi.Cells[0].Controls[1]);
                if (cb.Checked)
                {
                    ids += dgList.DataKeys[dgi.ItemIndex] + ",";
                }
            }

            if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
                var task = new Task();
                try
                {
                    task.DeleteTaskSubscription(ActualUsername, ids);

                    Response.Write("<script language=javascript>alert('操作成功!');window.location='TaskList.aspx?displayType=" + displayType + "';</script>");
                    //Response.AddHeader("Refresh","1");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }

        private void lnkbtnArranged_Click(object sender, EventArgs e)
        {
            var ActualUsername = (string)Session["ActualUsername"];
            var Username = (string)Session["Username"];
            //this.btnCancelSubscription.Visible = (ActualUsername.ToLower()==Username.ToLower());
            btnCancelSubscription.Visible = false;
            btnCancel.Visible = false;
            displayType = 5;
            tblContainer.Rows[0].Cells[3].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[0].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[1].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[2].Attributes.Add("background", "../../images/maillistbutton1.gif");
            tblContainer.Rows[0].Cells[4].Attributes.Add("background", "../../images/maillistbutton2.gif");
            var task = new Task();
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetAllTaskBySomeone(DateTime.Today.ToShortDateString(), Username, displayType));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            //	setgrid();
            mydb.Dispose();
        }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            Username = (string)Session["Username"];
            dgList.CurrentPageIndex = e.NewPageIndex;
            var task = new Task();
            DataTable mydb = Tools.ConvertDataReaderToDataTable(task.GetAllTaskBySomeone(DateTime.Today.ToShortDateString(), Username, displayType));
            dgList.DataSource = mydb.DefaultView;
            dgList.DataBind();
            setgrid();
        }

        #endregion

        #region 绑定数据至表格

        public void PopulateDateToTable(DateTime sDate, int dayCount)
        {
            var Username = (string)Session["Username"];
            string[] UnameStr = Regex.Split(Username, ",");
            var task = new Task();

            #region 初始化日程数据

            var TaskData = new int[1][];
            var htcolorlist = new Hashtable();
            // 初始化天数据至ArrayList
            //			ArrayList demoDayData = new ArrayList();	
            //			demoDayData.Add(9);
            //			demoDayData.Add(10);
            //			demoDayData.Add(11);
            //			demoDayData.Add(12);


            //			int[][] TaskData = new int[5][];  //每日的任务数据
            //			TaskData[0] = new int[]{0,0,0,0,0,13,14,15,16,17,18};
            //			TaskData[1] = new int[]{8,9,10,11,0,0,0,0,0,0,0};
            //			TaskData[2] = new int[]{0,9,10,11,12,13,0,0,0,0,0};
            //			TaskData[3] = new int[]{8,9,10,11,12,13,14,15,0,0,0};
            //			TaskData[4] = new int[]{0,0,0,0,0,0,0,0,0,17,18};
            //			demoData[5] = new int[]{0,0,0,0,0,0,0,0};
            //			demoData[6] = new int[]{0,0,0,0,0,0,0,0};
            //			demoData[7] = new int[]{0,0,0,0,0,0,0,0};
            // 生成行和单元格
            var DaySch = new ArrayList(dayCount + 1); //根据选择的天数生成数组 加1是因为会增加一信息列
            var DayTask = new ArrayList(dayCount + 1); //记录每天的前五条任务ID
            DaySch.Add(new int[5][]); //加入一个废列
            DayTask.Add(new String[5]);
            for (int p = 0; p < dayCount; p++)
            {
                TaskData = new int[5][];
                string tmpDate = sDate.AddDays(p).ToString();
                var a = new String[5];
                a = task.GetTop5DayTaskList(tmpDate, UnameStr[0]);

                //	TaskData[0] = new int[]{0,0,0,0,0,13,14,15,16,17,18};
                //				TaskData[1] = new int[]{8,9,10,11,0,0,0,0,0,0,0};
                //				TaskData[2] = new int[]{0,9,10,11,12,13,0,0,0,0,0};
                //				TaskData[3] = new int[]{8,9,10,11,12,13,14,15,0,0,0};
                //				TaskData[4] = new int[]{0,0,0,0,0,0,0,0,0,17,18};
                for (int k = 0; k < a.Length; k++)
                {
                    TaskData[k] = task.GetTaskPeriod(tmpDate, UnameStr[0], Int32.Parse(a[k]));
                }
                DaySch.Add(TaskData);
                DayTask.Add(a);
            }

            #endregion

            int numcells = dayCount;
            //	int startTimeNo = 8;	
            SqlDataReader dataReader = null;
            dataReader = task.GetPeriodInfo();
            var period = new ArrayList();
            while (dataReader.Read())
            {
                period.Add(dataReader[1].ToString());
            }
            int numrows = period.Count;


            //  初始化表头

            var r = new TableRow();
            // 生成一个第一列，列头为时间
            TableCell c = null;

            if (Username != "")
            {
                for (int i = 0; i < numcells + 1; i++)
                {
                    string s = sDate.AddDays(i - 1).ToShortDateString() == DateTime.Today.ToShortDateString() ? "<font color=white><b>" + DateTime.Today.ToShortDateString() + "</b></font>" : sDate.AddDays(i - 1).ToShortDateString();
                    var lc = new LiteralControl((i == 0) ? "<font color=white>&nbsp;&nbsp; 时段&nbsp;&nbsp;&nbsp;    " : "<font color=white>" + s + "&nbsp;&nbsp;&nbsp;" + Tools.ConvertDayOfWeekToZh(sDate.AddDays(i - 1).DayOfWeek) + "</font>");
                    if (i == numcells)
                    {
                        lc.Text += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=tasklist.aspx?SchBeginDate=" + DateTime.Parse(SchBeginDate).AddDays(-7).ToShortDateString() + "><font color=white size=3><</font></a>&nbsp;&nbsp;&nbsp; <a href=tasklist.aspx?SchBeginDate=" + DateTime.Parse(SchBeginDate).AddDays(7).ToShortDateString() + "><font color=white size=3>></font></a>";
                    }
                    c = new TableCell();
                    c.CssClass = "top";
                    c.BackColor = Color.FromName("#337FB2");
                    c.Controls.Add(lc);
                    r.Cells.Add(c);
                }
                Table1.Rows.Add(r);
                // 初始化表头结束

                // 根据时间段共生成8行
                for (int j = 0; j < numrows; j++)
                {
                    r = new TableRow();

                    // 每行根据选择的天数生成列
                    for (int i = 0; i < dayCount + 1; i++)
                    {
                        c = new TableCell();

                        var newtable = new Table();

                        if (i == 0)
                        {
                            if (j % 2 == 0)
                            {
                                string[] a = period[j].ToString().Split('-');
                                c.Controls.Add(new LiteralControl("<div style='position:absolute; width:86px; height:76px; z-index:1'><font size=3>" + a[0] + "</font></div>"));
                            }
                            else
                            {
                                c.Controls.Add(new LiteralControl("-"));
                            }
                        }
                        else
                        {
                            //如果不是第一列

                            //					newtable.BorderWidth = 0;
                            //newtable.GridLines = System.Web.UI.WebControls .GridLines.Vertical;
                            // 在某列中新table设置开始
                            newtable = new Table();
                            var nr = new TableRow();

                            for (int q = 0; q < TaskData.Length; q++)
                            {
                                var tmp = (int[][])DaySch[i];
                                var a = (String[])DayTask[i];
                                var nc = new TableCell();

                                bool flag = false;
                                //	string col = "";
                                string taskid = "";


                                //　遍历五个任务时间数组，看有没有被占
                                for (int co = 0; co < TaskData.Length; co++)
                                {
                                    if (Int32.Parse(tmp[co][j].ToString()) != 0)
                                    {
                                        flag = true;
                                        taskid = a[co];
                                    }
                                }

                                if (flag)
                                {
                                    nc.Controls.Add(new LiteralControl("<font color=#C597DD>0" + "</font>"));
                                    nc.BackColor = Color.FromName("#C597DD");
                                    nc.Style.Add("cursor", "hand");
                                    nc.Attributes.Add("onclick", "return dialwinprocess('" + sDate.AddDays(i - 1).ToShortDateString() + "','" + (8 + j) + "','2','" + taskid + "')");
                                }
                                else
                                {
                                    nc.Controls.Add(new LiteralControl("<font color=#FFFFFF>0</font>"));
                                }
                                nr.Cells.Add(nc);
                            }


                            newtable.CellPadding = 0;
                            newtable.CellSpacing = 0;
                            newtable.GridLines = GridLines.Both;
                            newtable.BorderWidth = 0;
                            newtable.Rows.Add(nr);
                            // 新table设置结束

                            c.Controls.Add(newtable);
                        }

                        r.Cells.Add(c);
                    }
                    Table1.CellPadding = 0;
                    Table1.CellSpacing = 0;
                    Table1.GridLines = GridLines.Horizontal;
                    Table1.BorderWidth = 1;
                    Table1.Rows.Add(r);
                }
            }
        }

        #endregion

        #region Nested type: demodat

        public class demodat
        {
            public int StartTime
            {
                //文件ID
                get;
                set;
            }

            public int EndTime
            {
                //文档ID
                get;
                set;
            }
        }

        #endregion
    }
}